package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;

/**
 * 对象功能: 客户名录查询
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/12 09:48
 */
public class CustomerListSearchFragment extends BaseFragment {
  protected static final String EXTRA_VLAUE =
      CustomerListSearchActivity.class.getSimpleName() + "EXTRA_VLAUE";

  @BindView(R.id.et_customer_name) EditText etCustomerName;
  @BindView(R.id.tv_customerCharge) TextView tvCustomerCharge;
  //@BindView(R.id.ib_customerCharge) ImageView ibCustomerCharge;
  @BindView(R.id.tv_customerStage) TextView tvCustomerStage;
  //@BindView(R.id.ib_customerStage) ImageView ibCustomerStage;

  private int REQUEST_CHARGE = 0;
  private int REQUEST_STAGE = 1;
  private String customerName;
  private String chargeManIds;
  private String customerStatus;
  private String chargeManNames;
  private String customerStatusName;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.topicQuery);
    ExtraValueContent content = (ExtraValueContent) getArguments().getSerializable(EXTRA_VLAUE);
    if (content == null) return;
    customerName = content.getExtName1();
    chargeManIds = content.getExtCode2();
    chargeManNames = content.getExtName2();
    customerStatus = content.getExtCode3();
    customerStatusName = content.getExtName3();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_customre_list_search, container, false);
    ButterKnife.bind(this, v);
    bindData();
    return v;
  }

  private void bindData() {
    etCustomerName.setText(customerName);
    tvCustomerCharge.setText(chargeManNames);
    tvCustomerStage.setText(customerStatusName);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      //查询
      case R.id.menu_item_confirm:
        volleyPoastSearch();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  //按条件查询订单
  private void volleyPoastSearch() {
    ExtraValueContent content = new ExtraValueContent();
    content.setExtName1(etCustomerName.getText().toString().trim());
    content.setExtCode2(chargeManIds);
    content.setExtName2(chargeManNames);
    content.setExtCode3(customerStatus);
    content.setExtName3(customerStatusName);

    Intent i = new Intent();
    i.putExtra(EXTRA_VLAUE, content);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @OnClick({ R.id.ib_customerCharge, R.id.ib_customerStage }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_customerCharge:
        //我方负责人:单选
        Intent charge = new Intent(getActivity(), SearchPersonActivity.class);
        charge.putExtra(SearchPersonListFragment.EXTRA_FROM, SearchPersonListFragment.VALUE_COMMON);
        startActivityForResult(charge, REQUEST_CHARGE);

        break;
      case R.id.ib_customerStage:
        showDialog(CustomerDialogFragment.newInstance(
            CustomerDialogFragment.VALUE_TYPE_CUSTOMER_UNIT_STATE), REQUEST_STAGE);
        break;
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_CHARGE) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(SearchPersonListFragment.EXTRA_VALUE);
      chargeManNames = dialog.getName();
      chargeManIds = dialog.getJobNo();
      tvCustomerCharge.setText(chargeManNames);
    }
    if (requestCode == REQUEST_STAGE) {
      CustomerDialog customerDialog =
          (CustomerDialog) data.getSerializableExtra(CustomerDialogFragment.EXTRA_VALUE);
      customerStatus = customerDialog.getId();
      customerStatusName = customerDialog.getName();
      tvCustomerStage.setText(customerStatusName);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static CustomerListSearchFragment newInstance(ExtraValueContent content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VLAUE, content);
    CustomerListSearchFragment fragment = new CustomerListSearchFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
